package com.zhangst.bbs.action;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.zhangst.bbs.domain.Commit;
import com.zhangst.bbs.domain.Content;
import com.zhangst.bbs.domain.User;
import com.zhangst.bbs.page.CommitModel;
import com.zhangst.bbs.page.ContentModel;
import com.zhangst.bbs.page.DataList;
import com.zhangst.bbs.service.CommitServiceI;
import com.zhangst.bbs.service.ContentServiceI;

public class CommitAction implements ModelDriven<CommitModel>{
	private CommitServiceI commitSerivce;
	private CommitModel commitModel = new CommitModel();
	private ContentServiceI contentService;
	
	
	public CommitServiceI getCommitSerivce() {
		return commitSerivce;
	}
	
	@Autowired
	public void setCommitSerivce(CommitServiceI commitSerivce) {
		this.commitSerivce = commitSerivce;
	}
	
	public ContentServiceI getContentService() {
		return contentService;
	}
	@Autowired
	public void setContentService(ContentServiceI contentService) {
		this.contentService = contentService;
	}

	@Action(value="page",results = { @Result(name = "success", location = "/WEB-INF/page/page.jsp") })
	public String page(){
		int content = commitModel.getContent();
		int page = commitModel.getPage();
		int rows= commitModel.getRows();
		if(page<=0){
			page=1;
		}
		
		if(rows<=0){
			rows =10;
		}
		DataList data = commitSerivce.getCommits(content, page, rows);
		Content content_ = contentService.getContent(content);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("commits", data);
		request.setAttribute("content", content_);
		return "success";
	}
	
	
	@Action(value="commit")
	public void commit() throws IOException{
		Commit commit = new Commit();
		Content content = new Content();
		content.setId(commitModel.getContent());
		commit.setContent(content);
		commit.setDate(new Timestamp(new Date().getTime()));
		commit.setCommitData(commitModel.getCommit());
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		User user = (User)session.get("user");
		if(user!=null){
			commit.setUser(user);
			commitSerivce.add(commit);
			
		}
		ServletActionContext.getResponse().sendRedirect("page.action?content="+content.getId());
	}
	
	
	
	@Action(value="delCommit")
	public void del() throws IOException{
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		User user = (User)session.get("user");
		if(user!=null&&user.getRole().getId()==2){
		Commit commit = new Commit();
		commit.setId(commitModel.getId());
		commitSerivce.del(commit);
		}
		ServletActionContext.getResponse().sendRedirect("page.action?content="+commitModel.getContent());
		
	}
	
	@Override
	public CommitModel getModel() {
		return commitModel;
	}
	
	
	
	
	
	
	
}
